package ch01;
import java.nio.CharBuffer;


public class BufferFillDrain {
	
	public static void main(String[] args) {
		CharBuffer charBuffer = CharBuffer.allocate(100);
		while (fillBuffer(charBuffer)) {
			charBuffer.flip();
			dranBuffer(charBuffer);
			charBuffer.clear();
		}
	}
	


	private static int index = 0;
	
	private static boolean fillBuffer(CharBuffer buffer){
		if(index >= strings.length){
			return false;
		}
		
		String item = strings[index++];
		buffer.put(item);
		return true;
	}
	
	private static void dranBuffer(CharBuffer charBuffer) {
		System.out.println(charBuffer.hasArray());
		System.out.println(charBuffer.array());
		
		while(charBuffer.hasRemaining()){
			System.out.print(charBuffer.get());
		}
		System.out.println();
	}
	
	public static String[] strings = {
		"A random string value",
		"The product of an infinite number of monkeys",
		"Hey hey we're the Monkess",
		"Opening me while I kiss the fly ",
		"Help me! Help me!"
	};

}
